app.controller("RegisterWithMail",function($scope,$http){

    var rightEmail = false;
    var rightPwd = false;
    var rightRePwd = false;
    var rightName = false;
    var rightIdentity = false;
    var rightAgree = false;

    $scope.judgeEmail = function () {
        if(!$scope.email){
            rightEmail = false;
            $scope.emailMsg = '邮箱不能为空！';
        }else if(!$scope.email.match(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/)){
            rightEmail = false;
            $scope.emailMsg = '邮箱格式错误！';
        }else{
            $http({
                method:'POST',
                data:{
                    email:$scope.email,
                },
                url:'../Register/judgeEmail'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightEmail = false;
                    $scope.emailMsg = '该邮箱已被注册';
                }else{
                    rightEmail = true;
                    $scope.emailMsg = '';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.judgeName = function () {
        if(!$scope.name){
            rightName = false;
            $scope.nameMsg = '昵称不能为空！';
        }else if($scope.name.length < 2 || $scope.name.length > 10){
            rightName = false;
            $scope.nameMsg = '限2~10字符，汉字或数字或字母（不区分大小写）及组合！';
        }else{
            $http({
                method:'POST',
                data:{
                    name:$scope.name,
                },
                url:'../Register/judgeName'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightName = false;
                    $scope.nameMsg = '该昵称已被使用';
                }else{
                    rightName = true;
                    $scope.nameMsg = '';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.judgePwd = function () {
        if (!$scope.password) {
            rightPwd = false;
            $scope.pwdMsg = '密码不能为空！';
        } else if (!$scope.password.match(/^[0-9a-zA-Z]{6,16}$/)) {
            rightPwd = false;
            $scope.pwdMsg = '限6~16个字符，字母(区分大小写)或数字及组合！';
        } else {
            rightPwd = true;
            $scope.pwdMsg = '';
        }
    };

    $scope.judgeRePwd = function () {
        if(!rightPwd){
            rightRePwd = false;
        } else if ($scope.repassword != $scope.password) {
            rightRePwd = false;
            $scope.rePwdMsg = '两次密码不一致，请检查！';
        } else {
            rightRePwd = true;
            $scope.rePwdMsg = '';
        }
    };

    $scope.judgeIdentity = function () {
        if (!$scope.role) {
            rightIdentity = false;
            $scope.subMsg = '请选择身份！';
        }else{
            rightIdentity = true;
            $scope.subMsg = '';
        }
    }

    $scope.judgeAgree = function () {
        if($scope.agree != true){
            rightAgree = false;
            $scope.subMsg = '请阅读并同意用户协议！';
        }else{
            rightAgree = true;
            $scope.subMsg = '';
        }
    }

    $scope.regWithMail = function(){
        if(!rightEmail || !rightPwd || !rightRePwd || !rightName){
            $scope.subMsg = '信息填写有误或不完整，请检查！';
            return;
        }
        if(!rightIdentity){
            $scope.subMsg = '请选择你的身份';
            return;
        }
        if(!rightAgree){
            $scope.subMsg = '请阅读并同意用户协议';
            return;
        }

        $http({
            method:'POST',
            data:{
                name:$scope.name,
                password:$scope.password,
                email:$scope.email,
                type:$scope.role.split(":")[1]
            },
            url:'../Register/AddUserWithMail'
        }).then(function successCallBack(response) {
            if(response.data == true){
                window.location="/zzlp/html/success.html";
            }else{
                alert("邮件发送失败");
            }
        },function errorCallBack(response) {

            alert("请求失败");
        })
    }

})

app.controller("RegisterWithTel",function($scope,$http,$timeout){

    var rightTel = false;
    var rightPwd = false;
    var rightRePwd = false;
    var rightName = false;
    var rightIdentity = false;
    var rightAgree = false;

    $scope.va = '免费获取验证码';

    $scope.judgeTel = function () {
        if(!$scope.tel){
            rightTel = false;
            $scope.telMsg = '手机号不能为空！';
        }else if(!$scope.tel.match(/^1[34578]\d{9}$/)){
            rightTel = false;
            $scope.telMsg = '手机号格式错误！';
        }else{
            $http({
                method:'POST',
                data:{
                    tel:$scope.tel
                },
                url:'../Register/judgeTel'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightTel = false;
                    $scope.telMsg = '该手机号已被注册';
                }else{
                    rightTel = true;
                    $scope.codeMsg = '';
                    $scope.telMsg = '';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.judgeName = function () {
        if(!$scope.name){
            rightName = false;
            $scope.nameMsg = '昵称不能为空！';
        }else if($scope.name.length < 2 || $scope.name.length > 10){
            rightName = false;
            $scope.nameMsg = '限2~10字符，汉字或数字或字母（不区分大小写）及组合！';
        }else{
            $http({
                method:'POST',
                data:{
                    name:$scope.name,
                },
                url:'../Register/judgeName'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightName = false;
                    $scope.nameMsg = '该昵称已被使用';
                }else{
                    rightName = true;
                    $scope.nameMsg = '';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.judgePwd = function () {
        if (!$scope.password) {
            rightPwd = false;
            $scope.pwdMsg = '密码不能为空！';
        } else if (!$scope.password.match(/^[0-9a-zA-Z]{6,16}$/)) {
            rightPwd = false;
            $scope.pwdMsg = '限6~16个字符，字母(区分大小写)或数字及组合！';
        } else {
            rightPwd = true;
            $scope.pwdMsg = '';
        }
    };

    $scope.judgeRePwd = function () {
        if(!rightPwd){
            rightRePwd = false;
        } else if ($scope.repassword != $scope.password) {
            rightRePwd = false;
            $scope.rePwdMsg = '两次密码不一致，请检查！';
        } else {
            rightRePwd = true;
            $scope.rePwdMsg = '';
        }
    };

    $scope.judgeIdentifyingCode = function () {
        if(!$scope.identifyingCode){
            $scope.codeMsg = '请输入短信验证码';
        }else if($scope.identifyingCode.length != 4){
            $scope.codeMsg = "验证码格式错误";
        }else{
            $scope.codeMsg = "";
        }
    }

    $scope.getIdentifyingCode = function () {
        if(!rightTel){
            $scope.codeMsg = '手机号未填写或填写有误！';
        }else{
            $http({
                method:'POST',
                data:{
                    tel:$scope.tel,
                },
                url:'../Register/sendMsg'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    $scope.codeMsg = '请输入短信验证码';
                    time();
                }else{
                    $scope.codeMsg = '短信发送失败';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.wait = 60;

    function time() {
        if ($scope.wait == 0) {
            $scope.disabled = false;
            $scope.va = '免费获取验证码';
            $scope.wait = 60;
        } else {
            $scope.disabled = true;
            $scope.va = '重新发送(' + $scope.wait + ')';
            $scope.wait--;
            $timeout(function () {
                time();
            }, 1000);
        }
    }

    $scope.judgeIdentity = function () {
        if (!$scope.role) {
            rightIdentity = false;
            $scope.subMsg = '请选择身份！';
        }else{
            rightIdentity = true;
            $scope.subMsg = '';
        }
    }

    $scope.judgeAgree = function () {
        if($scope.agree != true){
            rightAgree = false;
            $scope.subMsg = '请阅读并同意用户协议！';
        }else{
            rightAgree = true;
            $scope.subMsg = '';
        }
    }

    $scope.regWithTel = function(){
        if(!rightTel || !rightPwd || !rightRePwd || !rightName){
            $scope.subMsg = '信息填写有误或不完整，请检查！';
            return;
        }
        if(!rightIdentity){
            $scope.subMsg = '请选择你的身份';
            return;
        }
        if(!rightAgree){
            $scope.subMsg = '请阅读并同意用户协议';
            return;
        }

        $http({
            method:'POST',
            data:{
                tel:$scope.tel,
                identifyingCode:$scope.identifyingCode
            },
            url:'../Register/checkMsg'
        }).then(function successCallBack(response) {
            if(response.data == true){
                regWithTel();
            }else{
                $scope.subMsg = '验证码错误';
            }
        },function errorCallBack(response) {
            alert("请求失败");
        })
    }

    function regWithTel(){
        $http({
            method:'POST',
            data:{
                name:$scope.name,
                password:$scope.password,
                tel:$scope.tel,
                type:$scope.role.split(":")[1]
            },
            url:'../Register/AddUserWithTel'
        }).then(function successCallBack(response) {
            if(response.data == true){
                alert("注册成功");
                window.location="/zzlp/html/login.html";
            }else{
                alert("注册失败");
            }
        },function errorCallBack(response) {
            alert("请求失败");
        })
    }

})

app.controller("LoginController",function($scope,$http){

    $scope.login = function () {
        if($scope.accountNum == ""){
            $scope.subMsg = '账号不能为空！';
            return;
        }
        if($scope.password == ""){
            $scope.subMsg = '密码不能为空！';
            return;
        }
        var email;
        var tel;
        if($scope.accountNum.indexOf("@") >= 0){
            email = $scope.accountNum;
        }else{
            tel = $scope.accountNum;
        }
        $http({
            method:'POST',
            data:{
                email:email,
                tel:tel,
                password:$scope.password
            },
            url:'../Register/judgeLogin'
        }).then(function successCallBack(response) {
            if(response.data == true){
                window.location="/zzlp/html/main.html";
            }else{
                $scope.subMsg = '账号或密码错误！';
            }
        },function errorCallBack(response) {
            alert("请求失败");
        })
    }

    $scope.enterEvent = function(e) {
        var keycode = window.event?e.keyCode:e.which;
        if(keycode==13){
            $scope.login();
        }
    }
})

app.controller("ResetPwdWithMail",function ($scope, $http) {

    var rightEmail = false;

    $scope.judgeEmail = function () {
        if(!$scope.email){
            rightEmail = false;
            $scope.emailMsg = '邮箱不能为空！';
        }else if(!$scope.email.match(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/)){
            rightEmail = false;
            $scope.emailMsg = '邮箱格式错误！';
        }else{
            $http({
                method:'POST',
                data:{
                    email:$scope.email,
                },
                url:'../Register/judgeEmail'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightEmail = true;
                    $scope.emailMsg = '';
                }else{
                    rightEmail = false;
                    $scope.emailMsg = '该邮箱未注册';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.resetPwdWithMail = function(){
        if(!rightEmail){
            return;
        }
        $http({
            method:'POST',
            data:{
                email:$scope.email,
            },
            url:'../ChangePassword/emailPwdRepaire'
        }).then(function successCallBack(response) {
            if(response.data == true){
                window.location="/zzlp/html/success.html";
            }else{
                alert("邮件发送失败");
            }
        },function errorCallBack(response) {

            alert("请求失败");
        })
    }


})

app.controller("mailPwdReset",function ($scope, $http) {

    var rightPwd = false;
    var rightRePwd = false;

    $scope.judgePwd = function () {
        if (!$scope.password) {
            rightPwd = false;
            $scope.pwdMsg = '密码不能为空！';
        } else if (!$scope.password.match(/^[0-9a-zA-Z]{6,16}$/)) {
            rightPwd = false;
            $scope.pwdMsg = '限6~16个字符，字母(区分大小写)或数字及组合！';
        } else {
            rightPwd = true;
            $scope.pwdMsg = '';
        }
    };

    $scope.judgeRePwd = function () {
        if(!rightPwd){
            rightRePwd = false;
        } else if ($scope.repassword != $scope.password) {
            rightRePwd = false;
            $scope.rePwdMsg = '两次密码不一致，请检查！';
        } else {
            rightRePwd = true;
            $scope.rePwdMsg = '';
        }
    }

    $scope.mailPwdReset = function () {
        if(!rightPwd){
            $scope.pwdMsg = '密码填写不正确！';
            return;
        }
        if(!rightRePwd){
            $scope.rePwdMsg = '确认密码不正确！';
            return;
        }
        $http({
            method:'POST',
            data:{
                password:$scope.password
            },
            url:'../ChangePassword/emailPwdReset'
        }).then(function successCallBack(response) {
            if(response.data == true){
                alert("密码重置成功");
                window.location="/zzlp/html/login.html";
            }else{
                alert("重置失败，请尝试重新发送邮件");
            }
        },function errorCallBack(response) {

            alert("请求失败");
        })
    }

})

app.controller("ResetPwdWithTel",function ($scope, $http,$timeout) {

    var rightTel = false;
    var rightPwd = false;
    var rightRePwd = false;

    $scope.va = '免费获取验证码';

    $scope.judgeTel = function () {
        if(!$scope.tel){
            rightTel = false;
            $scope.telMsg = '手机号不能为空！';
        }else if(!$scope.tel.match(/^1[34578]\d{9}$/)){
            rightTel = false;
            $scope.telMsg = '手机号格式错误！';
        }else{
            $http({
                method:'POST',
                data:{
                    tel:$scope.tel
                },
                url:'../Register/judgeTel'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    rightTel = true;
                    $scope.codeMsg = '';
                    $scope.telMsg = '';

                }else{
                    rightTel = false;
                    $scope.telMsg = '该手机号未被注册';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.judgePwd = function () {
        if (!$scope.password) {
            rightPwd = false;
            $scope.pwdMsg = '密码不能为空！';
        } else if (!$scope.password.match(/^[0-9a-zA-Z]{6,16}$/)) {
            rightPwd = false;
            $scope.pwdMsg = '限6~16个字符，字母(区分大小写)或数字及组合！';
        } else {
            rightPwd = true;
            $scope.pwdMsg = '';
        }
    };

    $scope.judgeRePwd = function () {
        if(!rightPwd){
            rightRePwd = false;
        } else if ($scope.repassword != $scope.password) {
            rightRePwd = false;
            $scope.rePwdMsg = '两次密码不一致，请检查！';
        } else {
            rightRePwd = true;
            $scope.rePwdMsg = '';
        }
    };

    $scope.getIdentifyingCode = function () {
        if(!rightTel){
            $scope.codeMsg = '手机号未填写或填写有误！';
        }else{
            $http({
                method:'POST',
                data:{
                    tel:$scope.tel,
                },
                url:'../Register/sendMsg'
            }).then(function successCallBack(response) {
                if(response.data == true){
                    $scope.codeMsg = '请输入短信验证码';
                    time();
                }else{
                    $scope.codeMsg = '短信发送失败';
                }
            },function errorCallBack(response) {
                alert("请求失败");
            })
        }
    }

    $scope.wait = 60;

    function time() {
        if ($scope.wait == 0) {
            $scope.disabled = false;
            $scope.va = '免费获取验证码';
            $scope.wait = 60;
        } else {
            $scope.disabled = true;
            $scope.va = '重新发送(' + $scope.wait + ')';
            $scope.wait--;
            $timeout(function () {
                time();
            }, 1000);
        }
    }

    $scope.resetPwdWithTel = function(){
        if(!rightTel || !rightPwd || !rightRePwd){
            $scope.subMsg = '信息填写有误或不完整，请检查！';
            return;
        }

        $http({
            method:'POST',
            data:{
                tel:$scope.tel,
                identifyingCode:$scope.identifyingCode
            },
            url:'../Register/checkMsg'
        }).then(function successCallBack(response) {
            if(response.data == true){
                changePwdWithTel();
            }else{
                $scope.subMsg = '验证码错误';
            }
        },function errorCallBack(response) {
            alert("请求失败");
        })
    }

    function changePwdWithTel(){
        $http({
            method:'POST',
            data:{
                password:$scope.password,
                tel:$scope.tel,
            },
            url:'../ChangePassword/telPwdReset'
        }).then(function successCallBack(response) {
            if(response.data == true){
                alert("密码重置成功");
                window.location="/zzlp/html/login.html";
            }else{
                alert("重置失败");
            }
        },function errorCallBack(response) {
            alert("请求失败");
        })
    }

})
